﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;

namespace Core.Configuration.Helper
{
    public class AppSettings
    {
        //static IConfiguration _configuration;
        //static IConfiguration Configuration
        //{
        //    get
        //    {
        //        if (_configuration == null)
        //        {
        //            var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json", false, false);
        //            _configuration = builder.Build();
        //        }
        //        return _configuration;
        //    }
        //}
      
        public static IConfiguration Configuration { get; set; }
        static AppSettings()
        {
            //ReloadOnChange = true 当appsettings.json被修改时重新加载            
            Configuration = new ConfigurationBuilder()
            .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
            .Build();
        }
        /// <summary>
        /// 封装要操作的字符
        /// AppSettingsHelper.GetContent(new string[] { "JwtConfig", "SecretKey" });
        /// </summary>
        /// <param name="sections">节点配置</param>
        /// <returns></returns>
        public static string Get(params string[] sections)
        {
            try
            {

                if (sections.Any())
                {
                    return Configuration[string.Join(":", sections)];
                }
            }
            catch (Exception) { }

            return "";
        }
    }
}